﻿// ***********************************************************************
// Assembly         : MVC4Portal
// Author           : LJO+01 (Laurent Jordi)
// Created          : 06-05-2013
//
// Last Modified By : LJO+01 (Laurent Jordi)
// Last Modified On : 06-05-2013
// ***********************************************************************
// <copyright file="_LayoutViewControllers_Base.cs" company="Copyright © Innovacall 2011">
//     Copyright (c) Copyright © Innovacall 2011. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************

using System;
using System.Globalization;
using System.Web.Security;
using InnovaApp.Layers.DomainLayer;
using INNOVACALL.Framework.Common;
namespace InnovaApp.Portals.MVC4Portal.Controllers.ViewControllers.LayoutViewControllers
{
    /// <summary>
    /// Class _LayoutViewControllers_Base
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class _LayoutViewControllers_Base<T> : InnovaApp.Layers.CommonLayer.Controllers.ViewControllers.LayoutViewControllers._LayoutViewControllers_Base<T> where T : InnovaApp.Layers.CommonLayer.Models.ViewModels.LayoutViewModels._LayoutViewModels_Base
    {
        /// <summary>
        /// The _current user
        /// </summary>
        private MembershipUser _currentUser = null;

        /// <summary>
        /// The default language
        /// </summary>
        protected string DefaultLanguage = InnovaApp.Layers.CommonLayer.Controllers.Environment.DefaultLanguage;
        /// <summary>
        /// Singleton : Identified User
        /// </summary>
        /// <value>The current user.</value>
        protected MembershipUser CurrentUser
        {
            get
            {
                if (_currentUser == null)
                {
                    _currentUser = Membership.GetUser();
                }
                return _currentUser;
            }
        }
        /// <summary>
        /// Returns true if CurrentUser is not null and if he is in Admin role
        /// </summary>
        /// <value><c>true</c> if [admin mode]; otherwise, <c>false</c>.</value>
        protected bool AdminMode
        {
            get
            {
                bool toReturn = false;
                if (this.CurrentUser != null)
                {
                    toReturn = Roles.IsUserInRole(this.CurrentUser.UserName, "Admin") || Roles.IsUserInRole(this.CurrentUser.UserName, "Writer");
                }
                return toReturn;
            }
        }
        /// <summary>
        /// Gets the language with friendly URL.
        /// </summary>
        /// <param name="friendlyUrl">The friendly URL.</param>
        /// <returns>System.Int32.</returns>
        protected int GetLanguageWithFriendlyURL(string friendlyUrl)
        {
            string DefaultLanguage = InnovaApp.Layers.CommonLayer.Controllers.Environment.DefaultLanguage;
            int toReturn = -1;
            if (friendlyUrl == "/")
            {
                friendlyUrl = "/" + DefaultLanguage;
            }
            string culture = friendlyUrl.Substring(1, 5);
            toReturn = EnumTools.EnumTypeTo<int>(culture.Substring(0, 2));
            return toReturn;
        }
        /// <summary>
        /// Gets the language description with friendly URL.
        /// </summary>
        /// <param name="friendlyUrl">The friendly URL.</param>
        /// <returns>System.String.</returns>
        protected string GetLanguageDescriptionWithFriendlyURL(string friendlyUrl)
        {
            string toReturn = InnovaApp.Layers.CommonLayer.Controllers.Environment.DefaultLanguage;
            if (friendlyUrl != "/")
            {
                toReturn = friendlyUrl.Substring(1, 5);
            }
            return toReturn;
        }
        /// <summary>
        /// Localizes the dynamic script.
        /// </summary>
        /// <param name="language">The language.</param>
        internal void LocalizeDynamicScript(string language)
        {
            for (var i = 0; i < base.dynamicJSCollection.Length; i++)
            {
                base.dynamicJSCollection[i] = base.dynamicJSCollection[i].Replace("/ScriptsG/", string.Format("/ScriptsG/{0}/", language));
            }
        }
    }
}